<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class mrebuild extends MY_Model {

	var $theme_data = array(
		'layout_main' => 'layout',
		'left' => 'include/leftcp',
	);
	public $mytable = "game";
	public $primaryKey = "game_id";
	public $resCode = "GAME";

	function __construct() {
		parent::__construct();
	}
	
	function rebuild_category_count($cate_id = 0){
		$this->load->model("mcategory");
		$this->db->select("cat_id");
		
		if(is_array($cate_id) &&count($cate_id)  >0)
			$cates = $this->mcategory->where_in($cate_id,'cat_id');
		else if($cate_id >0)
			$cates = $this->mcategory->gets($cate_id);
		else
			$cates = $this->mcategory->gets();
		
		if($cates)
		foreach($cates as $cate){
			 $this->db->where('cat_id', $cate->cat_id);
			$this->db->from('game_category');
			$total = $this->db->count_all_results();
			
			$this->mcategory->update($cate->cat_id,'cat_id',array('cat_count'=>$total));
		}
	}
	function rebuild_category_catcode($cate_id = 0){
		$this->load->model("mcategory");
		$this->db->select("cat_id,cat_name");
		
		if(is_array($cate_id) &&count($cate_id)  >0)
			$cates = $this->mcategory->where_in($cate_id,'cat_id');
		else if($cate_id >0)
			$cates = $this->mcategory->gets($cate_id);
		else
			$cates = $this->mcategory->gets();
		
		if($cates)
		foreach($cates as $cate){
			$cat_name = $this->sseo->seolink($cate->cat_name);
			$this->mcategory->update($cate->cat_id,'cat_id',array('cat_code'=>$cat_name));
		}
	}
	function rebuild_game_score_rank($game_id = 0){
		$ranks = $this->db->query("SELECT game_id, use_id, gscore_score, @curRank := @curRank + 1 AS rank
FROM  game_score p, (SELECT @curRank := 0) r
where game_id = '".$game_id."' ORDER BY gscore_score desc, gscore_time_update asc")->result();
		$data = array();
		if($ranks){
			foreach ($ranks as $rank)
			{
				 $data[] = array(
				 'use_id'=> $rank->use_id,
				 'gscore_rank'=> $rank->rank,
				 );
			}
			$this->db->where("game_id", $game_id);
			$this->db->update_batch('game_score', $data, 'use_id'); 
		}
	}
	
	function rebuild_gamescore($game_id = 0){
		$ranks = $this->db->query("select use_id, max(gscore_scores) as max, gscore_time_create from game_scores 
		where game_id = ".$game_id."
		GROUP BY use_id")->result();
		$data = array();
		if($ranks){
			foreach ($ranks as $rank)
			{
				$data[] = array(
				'use_id'=> $rank->use_id,
				'gscore_score'=> $rank->max,
				'gscore_time_update'=> $rank->gscore_time_create,
				);
			}
			$this->db->where("game_id", $game_id);
			$this->db->update_batch('game_score', $data, 'use_id'); 
		}
	}
	
	function rebuild_user_rank($user_id = 0){
			$this->db->query("SET @rank = 0;");
		$this->db->query("SET @prev = 0;");
		$this->db->query("SET @curr = 0;");
		$ranks = $this->db->query("
			SELECT
 use_id , use_scores, 
  @prev := @curr,
  @curr := use_scores,
  @rank := IF(@prev = @curr, @rank, @rank+1) AS rank
FROM
  user
ORDER BY use_scores DESC")->result();
		$data = array();
		if($ranks){
			foreach ($ranks as $rank)
			{
				if(( is_array($user_id) && in_array($rank->use_id, $user_id)) || (!is_array($user_id) && $user_id > 0 && $rank->use_id == $user_id) || $user_id ==0)
					 $data[] = array(
					 'use_id'=> $rank->use_id,
					 'use_rank'=> $rank->rank,
					 );
			}
			if(count($data) > 0)
				$this->db->update_batch('user', $data, 'use_id'); 
		}
	}
	
	function rebuid_game_image_cache($game_id =''){
		$this->load->model("mgame");
		$this->load->model("simage");
		$this->db->select('game_thumb,game_thumb_large');
		if($game_id >0)
			$this->db->where('game_id',$game_id);
		$games= $this->mgame->gets();
		if($games){
			foreach($games as $game){
				$this->simage->delete(@$game->game_thumb);
				$this->simage->delete(@$game->game_thumb_large);
				$this->simage->cache(@$game->game_thumb);
				$this->simage->cache(@$game->game_thumb_large);
			}
		}
	}
	function fix_game_image_cache($game_id =''){
		echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
		$this->load->model("mgame");
		$this->load->model("simage");
		$this->db->select('game_thumb,game_thumb_large,game_title,game_id');
		if($game_id >0)
			$this->db->where('game_id',$game_id);
		$games= $this->mgame->gets();
		if($games){
			$image_size = $this->config->item("IMAGE_SIZE");
			foreach($games as $game){
				
				if(isset($image_size)){
					foreach($image_size as $kname =>$size){
						if(isset($size[0])&& isset($size[1])){
						
							$dir_cache = $this->simage->create_dirs($game->game_thumb);
							$file_name = basename($game->game_thumb);
							$full_patch = $dir_cache.$size[0]."x".$size[1]."-".$file_name;
							
							if(!file_exists($full_patch)){
								echo "fix thumb: ".$game->game_id.' - '.$game->game_title."<br>";
								$this->simage->run_cache($game->game_thumb,$size[0],$size[1]);
							}
							
							$dir_cache = $this->simage->create_dirs($game->game_thumb_large);
							$file_name = basename($game->game_thumb_large);
							$full_patch = $dir_cache.$size[0]."x".$size[1]."-".$file_name;
							
							if(!file_exists($full_patch)){
								echo "fix large: ".$game->game_id.' - '.$game->game_title."<br>";
								$this->simage->run_cache($game->game_thumb_large,$size[0],$size[1]);
							}
						}
					}
				}
			}
		}
	}
	function rebuid_user_avatar_cache($name =''){
		$this->load->model("muser");
		$this->load->model("simage");
		$this->db->select('use_avatar');
		$users= $this->muser->gets();
		if($users){
			foreach($users as $user){
				$this->simage->delete(@$user->use_avatar);
				$this->simage->cache(@$user->use_avatar);
			}
		}
	}
	function rebuid_game_file_rate($name =''){
		$this->load->model("mgame");
		$this->load->model("simage");
		$this->db->select('game_id, game_file');
		$games= $this->mgame->gets();
		$arr_update = array();
		if($games){
			foreach($games as $game){
				$file_size = getimagesize('./'.$game->game_file);
				$arr_update['game_file_rate_height_width'] = $file_size[1]/$file_size[0];
				if($arr_update['game_file_rate_height_width'] >0.7)
					$arr_update['game_file_rate_height_width'] = 0.7;
				$this->mgame->update($game->game_id,'game_id',$arr_update);
			}
		}
	}
	
	
	public function build_sitemap(){
		$arr_link = array();
		$arr_index = array();
		$start_time = time();
		$this->load->model("ssitemap");
		$arr_link['game'] = $this->ssitemap->game_detail();
		$arr_link['category'] = $this->ssitemap->category();
		//$arr_link['series'] = $this->ssitemap->series();
		
		//$arr_index = array_merge((array)$arr_link['game'], (array)$arr_link['category'], (array)$arr_link['series']);
		$arr_index = array_merge((array)$arr_link['game'], (array)$arr_link['category']);
		//$arr_index = $this->ssitemap->buid_gzip($arr_index,1);
		$sitemap_index = $this->ssitemap->build_index($arr_index);
		//$index_file = $this->ssitemap->buid_gzip($sitemap_index,1);
		//print_r($sitemap_index);
		echo "<br> <b>Gzip file index</b>: ".base_url().$sitemap_index[0]."<br>";
		$end_time = time() - $start_time;
		echo "total time: ".$end_time."s<br>";
	}
}

?>